<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: pp <124910168@qq.com>
// +----------------------------------------------------------------------

namespace controller;

use think\Db;
use think\Controller;
use think\Session;
use think\Request;
use think\Config;
use think\Env;
use clt\Leftnav;
use service\DataService;
use service\ToolsService;
use think\paginator\driver\Bootstrap;

/**
 * Home 端基类
 * Class BasicHome
 * @package controller
 * @author pp <124910168@qq.com>
 * @date 2020/05/30
 */
class BasicHome extends Controller{

    function _initialize()
    {
        parent::_initialize();
        
        $this->SU = Db::connect(config('dbx.SU'));

        $this->user = model('common/User');
        $this->member = model('common/Member');
        $this->client = model('common/Client');
        $this->area = model('common/Area');
        $this->nation = model('common/Nation');
        $this->disease = model('common/Disease');
        $this->category = model('common/Category');
        $this->article = model('common/Article');
        $this->characteristic = model('common/Characteristic');
        $this->clinic = model('common/Clinic');
        $this->prescription = model('common/Prescription');
        $this->drug = model('common/Drug');
        $this->demonstration = model('common/Demonstration');

        $this->controller = strtolower(request()->controller());
        $this->action = strtolower(request()->action());
        $this->assign('controller', $this->controller);
        $this->assign('action', $this->action);

        //民族+疾病
        $this->nationList = Db::name('Nation')->where(['status' => 1, 'is_deleted' => 0])->order('sort asc')->select();
        $this->assign('nationList', $this->nationList);
        
        $this->diseaseList = Db::name('Disease')->where(['status' => 1, 'is_deleted' => 0])->order('sort asc')->select();
        $this->assign('diseaseList', $this->diseaseList);

        //地区
        $this->areas = $this->SU->name('Area')->where(['area_open' => 1, 'area_deep' => 2])->select();
        $this->assign('areas', $this->areas);
        //民族
        $this->nations = $this->nation->getLists(['status' => 1], '', '', 1000);
        $this->assign('nations', $this->nations);
        //疾病
        $this->diseases = $this->disease->getLists(['status' => 1], '', '', 1000);
        $this->assign('diseases', $this->diseases);
        // halt($this->diseases);
        
        //课件分类
        $this->coCates = $this->category->getColumn(['id' => ['in', '2,3,4,5']], 'id,catname,moduleid,module');
        $this->assign('coCates', $this->coCates);
        
        //药品分类
        $this->drugCateList = Db::name('Drugcate')->where(['status' => 1, 'is_deleted' => 0])->order('id desc')->select();
        $this->assign('drugCateList', $this->drugCateList);
        
        //地区
        $areas = Db::name('Area')->order('area_id asc')->select();
        $this->aids = ToolsService::getArrSubIds($areas,1,'area_id','area_parent_id');//当前区域下地区Ids
        $areaList = $this->_getAreaTrees('Area', ['area_open' => 1, 'area_id' => ['in', $this->aids]]);
        $this->assign('areaList', $areaList);
        
        $usergroup = config('dbx.member_auth');
        $this->assign('rlist',$usergroup);

        // if(input('aid')){
        //     session('aid', input('aid'));
        // }
        // //地区id，优先顺序：参数aid > session('aid') > session('member.aid') > 1
        // $this->aid = session('aid') ? session('aid') : (session('member.aid') ? session('member.aid') : 1);
        // $this->assign('area_name', $this->area->where(['area_id' => $this->aid])->value('area_name'));
        // $this->assign('aid', $this->aid);
        // $this->aids = _getAIDs($this->aid);
        // // halt($this->aid);
        
        // //栏目map
        // $this->map = [
        //     'aid' => ['in', $this->aids],
        //     'status' => 0,
        //     'thumb' => ['neq', ''],
        // ];

        //轮播
        $banners = get_banner(2, 5);
        // halt($banners);
        $this->assign('banners', $banners);
    }

    /***
     * @param $data   传入的数组
     * @param int $listRow // 默认为config的参数
     *  @param string $name // 在模版循环的渲染的名称
     *   @param string $page // 在模版分页显示
     */

    public function mypage($data, $listRow='', $name = 'list', $page = 'page'){
        if (!is_array($data)||empty($data)||empty($name)||empty($page)){
            return false;
        }
        if (empty($listRow)){
            $listRow= intval(config('paginate')['list_rows']);  //获取config里面的参数
        }

        $curPage = input('page') ? input('page') : 1;//当前第x页，有效值为：1,2,3,4,5...
        $showData = array_slice($data, ($curPage - 1)*$listRow, $listRow,true);

        $p = Bootstrap::make($showData, $listRow, $curPage, count($data), false, [
            'var_page' => 'page',

            'path'     => url($this->url),
            'fragment' => '',
        ]);
        $p->appends($_GET);
        // halt($p);
        return $p;
        // $this->assign($name, $p);
        // $this->assign($page, $p->render());
    }

    //权限判断
    public function _ckR(){
        $member = session('member');
        if(!$member){
            $this->error('请先登录', url('home/login/index'));
        }
        // halt($member);
        if(!in_array($member['role'], [2,3])){
            $this->error('权限不足');
        }
    }
    //地区节点（带上下级）
    public function _getAreaTrees($table = 'Area', $where = ['area_open' => 1]) {
        $_menus = Db::connect($this->conSU)->name($table)->where($where)->order('area_sort asc,area_id asc')->select();

        $menus = ToolsService::arr2table($_menus,'area_id','area_parent_id');
        foreach ($menus as $key => &$menu) {

            if (substr_count($menu['path'], '-') > 4) {
                unset($menus[$key]);
                continue;
            }
            if (isset($vo['area_parent_id'])) {
                $current_path = "-{$vo['area_parent_id']}-{$vo['area_id']}";
                if ($vo['area_parent_id'] !== '' && (stripos("{$menu['path']}-", "{$current_path}-") !== false || $menu['path'] === $current_path)) {
                    unset($menus[$key]);
                }
            }
        }
        return $menus;
    }
}